﻿using ETask.Api.Data;
using ETask.Api.Dtos;
using ETask.Api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ETask.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuthController : ControllerBase
    {

        private readonly IUserService _userService;
        private readonly IAuditLogService _auditLogService;

        public AuthController(IUserService userService, IAuditLogService auditLogService)
        {
            _userService = userService;
            _auditLogService = auditLogService;
        }

        [HttpPost("login")]
        public async Task<IActionResult> Login([FromBody] LoginRequest request)
        {
            try
            {
                ApiResponse apiResponse = await _userService.Login(request);
                
                // 记录登录日志
                if (apiResponse.Code == 200)
                {
                    await _auditLogService.LogSuccessAsync("登录", "用户", $"用户 {request.Account} 登录成功");
                }
                else
                {
                    await _auditLogService.LogErrorAsync("登录", "用户", $"用户 {request.Account} 登录失败", apiResponse.Message);
                }
                
                return Ok(apiResponse);
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("登录", "用户", $"用户 {request.Account} 登录异常", ex.Message);
                return Ok(ApiResponse.Fail(500, "登录失败"));
            }
        }

        [HttpPost("userInfo")]
        [Authorize]
        public async Task<IActionResult> UserInfo()
        {
            var res= await _userService.UserInfo();
            return Ok(res);
        }

        [HttpPost("resetPassword")]
        [Authorize]
        public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordDto reset)
        {
            ApiResponse apiResponse = await _userService.ResetPassword(reset);
            return Ok(apiResponse);
        }

        [HttpPost("updateInfo")]
        [Authorize]
        public async Task<IActionResult> UpdateInfo([FromBody] UpdateUserDto updateUserDto)
        {
            try
            {
                ApiResponse apiResponse = await _userService.UpdateUserInfo(updateUserDto);
                
                // 记录用户信息修改日志
                if (apiResponse.Code == 200)
                {
                    await _auditLogService.LogSuccessAsync("修改", "用户", $"用户修改个人信息", updateUserDto.Id, updateUserDto.Name);
                }
                else
                {
                    await _auditLogService.LogErrorAsync("修改", "用户", $"用户修改个人信息失败", apiResponse.Message, updateUserDto.Id, updateUserDto.Name);
                }
                
                return Ok(apiResponse);
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("修改", "用户", $"用户修改个人信息异常", ex.Message, updateUserDto.Id, updateUserDto.Name);
                return Ok(ApiResponse.Fail(500, "修改失败"));
            }
        }

        [HttpPost("register")]
        public async Task<IActionResult> Register([FromBody] CreateUserDto createUserDto)
        {
            try
            {
                var res = await _userService.Register(createUserDto);
                
                // 记录用户注册日志
                if (res.Code == 200)
                {
                    await _auditLogService.LogSuccessAsync("创建", "用户", $"用户 {createUserDto.Name} 注册成功");
                }
                else
                {
                    await _auditLogService.LogErrorAsync("创建", "用户", $"用户 {createUserDto.Name} 注册失败", res.Message);
                }
                
                return Ok(res);
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("创建", "用户", $"用户 {createUserDto.Name} 注册异常", ex.Message);
                return Ok(ApiResponse.Fail(500, "注册失败"));
            }
        }

    }
}
